# # Load necessary libraries
# library(dplyr)
# library(tidyr)
# library(ggplot2)
# library(scales)

# Set country list and labels
c_list <- c("AU_Central", "AU_East", "AU_West", "AU_Southern")
c_labels <- c("Central Africa", "East Africa", "West Africa", "Southern Africa")

# Prepare the data
Savings_Bank_only_vs_MM_Frequency <- data %>% 
  filter(group == "all") %>% 
  filter(year == 2024) %>% 
  filter(countrynewwb %in% c_list) %>%
  select(countrynewwb, year, fin_sav_fionly_freq_w,
         fin_sav_fionly_freq_m, fin_sav_fionly_freq_lm,
         fin_sav_mm_freq_w, fin_sav_mm_freq_m, fin_sav_mm_freq_lm) %>% 
  rename(
    bank_account_only_w = fin_sav_fionly_freq_w,
    bank_account_only_m = fin_sav_fionly_freq_m,
    bank_account_only_lm = fin_sav_fionly_freq_lm,
    mobile_money_account_w = fin_sav_mm_freq_w,
    mobile_money_account_m = fin_sav_mm_freq_m,
    mobile_money_account_lm = fin_sav_mm_freq_lm
  ) %>% 
  filter(!is.na(bank_account_only_w))

# Reshape data to long format
Savings_Bank_only_vs_MM_Frequency_long_data <- Savings_Bank_only_vs_MM_Frequency %>%
  pivot_longer(
    cols = starts_with("bank_account_only") | starts_with("mobile_money_account"),
    names_to = c("type", "category"),
    names_pattern = "(.*)_(.*)",
    values_to = "value"
  ) %>%
  mutate(
    value = value * 100,
    type = factor(type, levels = c("bank_account_only", "mobile_money_account")),
    category = factor(category, levels = c("lm", "w", "m"),
                      labels = c("Less than once a month", "Weekly", "Monthly")),
    fill_group = case_when(
      type == "bank_account_only" & category == "Less than once a month" ~ "Bank_LessMonthly",
      type == "mobile_money_account" & category == "Less than once a month" ~ "MM_LessMonthly",
      type == "bank_account_only" & category == "Weekly" ~ "Bank_Weekly",
      type == "mobile_money_account" & category == "Weekly" ~ "MM_Weekly",
      type == "bank_account_only" & category == "Monthly" ~ "Bank_Monthly",
      type == "mobile_money_account" & category == "Monthly" ~ "MM_Monthly"
    ),
    fill_group_label = case_when(
      type == "bank_account_only" ~ "FI Only",
      type == "mobile_money_account" ~ "Mobile Money Account"
    )
  ) %>%
  filter(value != 0)

# Color mapping
color_mapping <- c(
  "Bank_LessMonthly" = "#B2D6F7",
  "MM_LessMonthly" = "#FCD6E8",
  "Bank_Weekly" = "#0F72BC",
  "MM_Weekly" = "#D12891",
  "Bank_Monthly" = "#5696D0",
  "MM_Monthly" = "#F087B6"
)

# Relabels for clean legend
legend_labels <- c(
  "Bank_LessMonthly" = "Less than once a month",
  "MM_LessMonthly" = "Less than once a month",
  "Bank_Weekly" = "Weekly",
  "MM_Weekly" = "Weekly",
  "Bank_Monthly" = "Monthly",
  "MM_Monthly" = "Monthly"
)

# Plot
ggplot(Savings_Bank_only_vs_MM_Frequency_long_data) +
  geom_bar(
    aes(x = factor(type), 
        y = value, 
        fill = fill_group),
    stat = "identity",
    position = "stack",
    width = 0.75,
    color = "black",    
    size = 0.3          
  ) +
  scale_fill_manual(
    values = color_mapping,
    labels = legend_labels,
    guide = guide_legend(
      title.position = "top",
      title.hjust = 0.5
    )
  ) +
  scale_y_continuous(
    labels = scales::comma_format(), 
    breaks = seq(0, 35, 5),
    name = NULL
  ) +
  facet_grid(
    . ~ factor(countrynewwb, levels = c_list, labels = c_labels),
    scales = "free",
    space = "free",
    switch = "both",
    labeller = label_wrap_gen(width = 10, multi_line = TRUE)
  ) +
  scale_x_discrete(
    labels = c(
      "bank_account_only" = "Bank or similar\nfinancial institution\nonly",
      "mobile_money_account" = "Mobile money"
    )
  ) +
  theme(
    strip.placement = "outside",
    strip.text.y.left = element_text(angle = 0),
    strip.text.y = element_text(size = 16, family = "Nunito Sans"),
    panel.background = element_blank(),
    strip.background = element_blank(),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank(),
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.border = element_blank(),
    legend.title = element_blank(),
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.x = element_text(family = "Nunito Sans", color = "black", size = 12),
    axis.text.y = element_text(family = "Nunito Sans", color = "black", size = 10),
    axis.ticks.x = element_blank(),
    axis.ticks.y = element_line(),
    plot.subtitle = element_text(size = 16, family = "Nunito Sans"),
    plot.title = element_text(hjust = 0, size = 16, family = "Nunito Sans", face = "bold"),
    plot.title.position = "plot",
    legend.text = element_text(size = 14, family = "Nunito Sans"),
    legend.position = "bottom",
    legend.justification = "center",
    legend.direction = "vertical",
    strip.text.x = element_text(family = "Nunito Sans", color = "black", size = 14),
    plot.caption = element_text(size = 12, hjust = 0, family = "Nunito Sans"), # Left-aligned caption
    plot.caption.position = "plot"
  ) +
  guides(fill = guide_legend(
    ncol = 2, 
    byrow = TRUE,
    override.aes = list(shape = NA)
  )) +
  labs(
    subtitle = "Adults saving formally in the past year by method (%), 2024",
    title = "In Sub-Saharan Africa, adults who saved using a mobile money account saved more frequently than adults who\nsaved using a bank or similar account",
    caption = "Source: Global Findex Database 2025"  # Caption added
  )

# Save plot
ggsave(
  filename = file.path(folder_path, "3.1.8.png"),
  width = 20,
  height = 8,
  units = "in",
  device = 'png',
  dpi = 120
)
